package com.wanjia.estate.utils.bank;

import cn.hutool.core.util.StrUtil;
import com.wanjia.estate.constant.bank.CodeCreator;
import com.wanjia.estate.constant.bank.CodeReason;
import com.wanjia.estate.exception.ControllerException;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import java.util.Objects;
import java.util.stream.Stream;

@NoArgsConstructor(access = AccessLevel.PRIVATE)
public final class CodeUtil {

    private static final String ILLEGAL_CODE = "不合法的应答码";

    public static CodeCreator getCreator(String code) {
        if (StrUtil.isEmpty(code) || code.length() != 10) {
            throw new ControllerException(ILLEGAL_CODE);
        }
        String key = code.substring(0, 2);
        return Stream.of(CodeCreator.values())
                .filter(e -> Objects.equals(e.code(), key))
                .findFirst()
                .orElse(null);
    }

    public static CodeReason getReason(String code) {
        if (StrUtil.isEmpty(code) || code.length() != 10) {
            throw new ControllerException(ILLEGAL_CODE);
        }
        String key = code.substring(2, 10);
        return Stream.of(CodeReason.values())
                .filter(e -> Objects.equals(e.code(), key))
                .findFirst()
                .orElse(null);
    }
}
